package com.neu.his.department.serviceimpl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.neu.his.department.entity.Department;
import com.neu.his.department.entity.DepartmentVo;
import com.neu.his.department.listener.DepartmentListener;
import com.neu.his.department.mapper.DepartmentMapper;
import com.neu.his.department.service.DepartmentService;
@Service
public class DepartmentServiceImpl implements DepartmentService{
@Autowired
	private DepartmentMapper departmentMapper;

@Override
public boolean addDepartment(Department department) {
	// TODO Auto-generated method stub
	int addDepartment = departmentMapper.addDepartment(department);
	boolean flag = false;
	if(addDepartment>0) {
		flag = true;
	} return flag;
}

@Override
public boolean deleteDepartment(int id) {
	// TODO Auto-generated method stub
	int deleteDepartment = departmentMapper.deleteDepartment(id);
	boolean flag=false;
	if(deleteDepartment>0) {
		 flag = true;
	}return flag;
}

@Override
public boolean deleteDepartmentByIds(Integer[] ids) {
	// TODO Auto-generated method stub
	int deleteDepartmentByIds = departmentMapper.deleteDepartmentByIds(ids);
	boolean flag=false;
	if(deleteDepartmentByIds>0) {
		 flag = true;
	}return flag;
}

@Override
public boolean updateDepartment(Department department) {
	// TODO Auto-generated method stub
	int updateDepartment = departmentMapper.updateDepartment(department);
	boolean flag=false;
	if(updateDepartment>0) {
		 flag = true;
	}return flag;
}

@Override
public List<DepartmentVo> pageDepartmentQuery(int page, int limit, String keyword, String keyword2, int keyword3) {
	// TODO Auto-generated method stub
	return departmentMapper.pageDepartmentQuery(page, limit, keyword, keyword2, keyword3);
}

@Override
public int pageDepartmentTotal(String keyword, String keyword2, int keyword3) {
	// TODO Auto-generated method stub
	return departmentMapper.pageDepartmentTotal(keyword, keyword2, keyword3);
		/* return 0 ; */
}

@Override
public void importExcel(MultipartFile file) {
	// TODO Auto-generated method stub
	
	// 导入excel
			try {
				InputStream inputStream = file.getInputStream();

				EasyExcel.read(inputStream, Department.class, new DepartmentListener(departmentMapper)).sheet("科室信息").doRead();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}



@Override
public void exportExcel(HttpServletResponse response) {
	// TODO Auto-generated method stub
	 try {
 		response.setContentType("application/vnd.ms-excel");
         response.setCharacterEncoding("utf-8");
			String fileName = URLEncoder.encode("科室信息", "UTF-8");
			 response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
			 
			 try {
				OutputStream outputStream = response.getOutputStream();
				 EasyExcel.write(outputStream, Department.class).sheet("科室信息").doWrite(departmentMapper.findAll());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

}
	




}
